package cn.itcast.tanhua.api;

import cn.itcast.model.User;
import cn.itcast.tanhua.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author Administrator
 */
@DubboService
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByMobile(String mobile) {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getMobile,mobile);
        return userMapper.selectOne(qw);
    }

    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public String update(User user) {
        userMapper.updateById(user);
        return null;
    }

    @Override
    public List<User> findAll() {
        return userMapper.selectList(Wrappers.emptyWrapper());
    }

    @Override
    public User findById(Long userId) {
        LambdaQueryWrapper<User> wq = new LambdaQueryWrapper<>();
        wq.eq(User::getId,userId);
        return userMapper.selectOne(wq);
    }

    @Override
    public User findByHuanxin(String huanxinId) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("hx_user",huanxinId);
        return userMapper.selectOne(qw);
    }

    /**
     * 保存修改手机号
     * @param phone
     * @return
     */
    @Override
    public void phonetest(Long userId, String phone) {
        User user = new User();
        user.setId(userId);
        user.setMobile(phone);

        userMapper.updateById(user);
    }
}